//
//  ComicGridViewController.m
//  BiDongComic
//
//  Created by 吴炳毅 on 16/3/11.
//  Copyright © 2016年 BestRiven. All rights reserved.
//

///每周排行榜
#define HOTURLSTR @"http://api.kuaikanmanhua.com/v1/topic_lists/mixed"
///轮播图
#define SFURLSTR @"http://api.kuaikanmanhua.com/v1/banners"
///
#define XQURLSTR @"http://api.kuaikanmanhua.com/v1/comics/10722"

#import "ComicGridViewController.h"
#import "ClassifyViewController.h"
#import "AFNetworking/AFNetworking.h"
#import "ComicGridModel.h"
#import "HotTableViewCell.h"
#import "HotScrollView.h"
#import "SFModel.h"
#import "WheelImageViewHelper.h"
#import "ListViewController.h"



@interface ComicGridViewController ()<UITableViewDataSource,UITableViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate> {
    ///每周排行榜
    NSMutableArray *arrAllData;
    ///分组数据
    NSMutableArray *sesionAllData;
    ///每行cell数据个数
    NSMutableArray *countArr;
    ///轮播图数据
    NSMutableArray *sfArrayAllData;
    ///判断按钮点击状态变量
    BOOL buttonBool;
    
}


@property (nonatomic, strong) HotScrollView *hotScrollView;


@end


@implementation ComicGridViewController


-(void)loadView {
    self.comicGridView = [[ComicGridView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.view = self.comicGridView;
}


- (void)viewWillAppear:(BOOL)animated {
    self.button_hot.hidden = NO;
    self.button_classify.hidden = NO;
}






- (void)viewDidLoad {
    [super viewDidLoad];
    //设置navagitionBar按钮
    [self setNavigationBar];
    //设置代理
    [self setDelegate];
    //设置视图
    [self setViews];
    //数据请求
    [self setData];

}






#pragma mark - setNavigationBar 

- (void)setNavigationBar {
    //初始化标签按钮 button_hot
    self.button_hot = [UIButton buttonWithType:UIButtonTypeSystem];
    self.button_hot.frame = CGRectMake(kScreenWidth/2 - 60, self.navigationController.navigationBar.frame.size.height/2-15, 60, 30);
    [self.button_hot setTitle:@"热门" forState:UIControlStateNormal];
    self.button_hot.titleLabel.textColor = [UIColor blackColor];
    [self.navigationController.navigationBar addSubview:self.button_hot];
    [self.button_hot setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [self.button_hot addTarget:self action:@selector(hot_action) forControlEvents:(UIControlEventTouchUpInside)];
    
    self.button_classify = [UIButton buttonWithType:UIButtonTypeSystem];
    self.button_classify.frame = CGRectMake(kScreenWidth/2 , self.navigationController.navigationBar.frame.size.height/2-15, 60, 30);
    [self.button_classify setTitle:@"分类" forState:UIControlStateNormal];
    self.button_classify.titleLabel.textColor = [UIColor blackColor];
    [self.navigationController.navigationBar addSubview:self.button_classify];
    [self.button_classify addTarget:self action:@selector(classify_action) forControlEvents:UIControlEventTouchUpInside];
    
    

    
}




#pragma mark - 代理设置
- (void)setDelegate {
    self.comicGridView.tableView_hot.delegate = self;
    self.comicGridView.tableView_hot.dataSource = self;
    [self.comicGridView.tableView_hot registerClass:[HotTableViewCell class] forCellReuseIdentifier:@"tableViewCell_id"];
}


#pragma mark - 数据请求
- (void)setData {
    
    //所有漫画数据列表
    AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
    [session GET:HOTURLSTR parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        arrAllData = [NSMutableArray array];
        sesionAllData = [NSMutableArray array];
        countArr = [NSMutableArray array];
        for (NSDictionary *dic in responseObject[@"data"][@"topics"]) {
            sesionAllData = dic[@"topics"];
            NSString *stringCount = [NSString stringWithFormat:@"%ld",sesionAllData.count];
            if (![stringCount isEqualToString:@"0"]) {
                [countArr addObject:stringCount];

            }
            for (NSDictionary *d in dic[@"topics"]) {
                ComicGridModel *model = [[ComicGridModel alloc]init];

                [model setValuesForKeysWithDictionary:d];
                [arrAllData addObject:model];
            }
        }
        //回到主线程 给ui赋值
        [self performSelectorOnMainThread:@selector(mainAction) withObject:nil waitUntilDone:YES];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"失败");
    }];
    
    //轮播图数据
    sfArrayAllData = [NSMutableArray array];
    AFHTTPSessionManager *session1 = [AFHTTPSessionManager manager];
    [session1 GET:SFURLSTR parameters:nil success:^(NSURLSessionDataTask *task, id responseObject){
        for (NSDictionary *dic in responseObject[@"data"][@"banner_group"]) {
            SFModel *model = [[SFModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [sfArrayAllData addObject:model.pic];
        }
        //回到主线程
        [self performSelectorOnMainThread:@selector(wheelImageMainAction) withObject:nil waitUntilDone:YES];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
}


#pragma mark -主线程刷新ui
- (void)mainAction {
    [self.comicGridView.tableView_hot reloadData];
    self.HUD.hidden = YES;
}

- (void)wheelImageMainAction {
    WheelImageViewHelper *wheelImageViewHelper = [[WheelImageViewHelper alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 200) array:sfArrayAllData timeInterval:3 animateDuration:0.5];
    self.comicGridView.tableView_hot.tableHeaderView =wheelImageViewHelper;
    
}

#pragma mark -setViews 视图设置

- (void)setViews {
    buttonBool = YES;
    [self.button_classify setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
}










#pragma mark - tableView代理方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    if (indexPath.row == 4) {
//        //官方活动
//        UITableViewCell *cell = [[UITableViewCell alloc]init];
//        return cell;
//        
//    }
    //初始化cell
    HotTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableViewCell_id" forIndexPath:indexPath];
    //取出每行个数数组
    NSString *string = countArr[indexPath.row];
    //添加scorllew的图片
    self.hotScrollView = [[HotScrollView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 170) arr:arrAllData index:indexPath imageCount:string.integerValue arrCount:countArr];
    //添加scrollView到cell
    [cell.contentView addSubview:self.hotScrollView];
    //初始化每组标题
    NSArray *arrTitle = [[NSArray alloc]initWithObjects:@"人气飙升",@"每周排行榜",@"新作出炉",@"主编力推",@"完结作品",@"所有作品" ,nil];
    //创建标题cell
    if (!cell.table_title) {
    cell.table_title = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, kScreenWidth/4, 30)];
    }
    cell.table_title.text = arrTitle[indexPath.row];

    cell.table_title.textColor = [UIColor lightGrayColor];
    cell.table_title.font = [UIFont systemFontOfSize:14];
    [cell.contentView addSubview:cell.table_title];
    //创建更多按钮
    if (!cell.button_more) {
        cell.button_more = [UIButton buttonWithType:UIButtonTypeSystem];
    }

    cell.button_more.tag = 200+indexPath.row;
    cell.button_more.frame = CGRectMake(kScreenWidth-60, 0, 60, 30);
    [cell.button_more addTarget:self action:@selector(moreAction:) forControlEvents:UIControlEventTouchUpInside];
    [cell.button_more setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    cell.button_more.titleLabel.font = [UIFont systemFontOfSize:15];
    [cell.contentView addSubview:cell.button_more];
        [cell.button_more setTitle:@"查看更多" forState:UIControlStateNormal];

    
    return cell;
}

//cell行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    //如果数据没有请求  不加载cell
    if (arrAllData.count == 0) {
        return 0;
    }
    return 6;
}

//tableView Cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 180;
}




//button hot点击事件
- (void)hot_action {
    buttonBool = YES;
    [self.navigationController popToRootViewControllerAnimated:YES];
    [self.button_classify setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.button_hot setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    self.button_classify.selected = NO;

}


- (void)classify_action {
    ClassifyViewController *classifyVC = [[ClassifyViewController alloc]init];
    self.navigationItem.hidesBackButton = YES;
    if (buttonBool) {
        [self.navigationController pushViewController:classifyVC animated:YES];
       

    }
    [self.button_hot setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.button_classify setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    buttonBool = 0;
    
}


//点击查看更多 按钮动作
- (void)moreAction:(UIButton *)sender {
    ListViewController *listVC = [[ListViewController alloc]init];
    listVC.more_select = 1;

    switch (sender.tag) {
        case 200:
            listVC.more_id = @"4";
            listVC.naVTitle = @"人气飙升";
            break;
        case 201:
            listVC.more_id = @"1";
            listVC.naVTitle = @"每周排行榜";
            break;
        case 202:
            listVC.more_id = @"5";
            listVC.naVTitle = @"新作出炉";
            break;
        case 203:
            listVC.more_id = @"3";
            listVC.naVTitle = @"主编力推";
            break;
        case 204:
            listVC.more_id = @"2";
            listVC.naVTitle = @"完结作品";
            break;
        case 205:
            listVC.more_id = @"others";
            listVC.naVTitle = @"所有作品";
            break;
        default:
            break;
    }
    [self.navigationController pushViewController:listVC animated:YES];
    
    self.button_hot.hidden = YES;
    self.button_classify.hidden = YES;
    

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
